﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using QD.Utill;
using QD.Snapshot.Core.Config;
using NSoup.Nodes;
using System.Text.RegularExpressions;
using NSoup.Select;
using QD.Snapshot.Core.Util;
using System.Net;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace QD.Snapshot.Core.BLL
{
    public class Search_Baidu : SearchBase
    {
        public Search_Baidu(TextBox textBox, int SleepTime)
        {
            this.textBox = textBox;
            this.SleepTime = SleepTime;
            this.copyform = "0";        //百度搜索引擎
            Echo("百度移动截图已启动");
        }

        HttpRequest Http = new HttpRequest();
        string htmls, msg;
        Model.TaskModel taskModel = new Model.TaskModel();
        public override void Search(string keyword, Model.Product product, Model.WebSite company)
        {
            Thread.Sleep(SleepTime);
            Http.UserAgent = "Mozilla/5.0 (Linux; Android 8.1; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36";

            if (string.IsNullOrEmpty(htmls))
                Http.Request_BD("http://m.baidu.com", out msg);
            for (int i = 1; i <= 3; i++)
            {
                htmls = string.Empty;
                Http.Referer = string.Format(Engine.URL_BD, StringHelper.ConvertUrl(keyword), (i - 1) * 10);
                msg = Http.Request_BD(string.Format(Engine.URL_BD, keyword, (i - 1) * 10), out htmls);
                IsCode_BD = CheckCode(htmls);   //是否出现验证码
                if (msg.Length > 0)
                {
                    Echo(string.Format("搜索 {0} 验证码", keyword));
                }
                else if (IsCode_BD)
                {
                    EchoLog("code", "百度出现验证码");
                    Thread.Sleep(1000);
                }
                else
                {
                    Echo(string.Format("搜索 {1}页 {0}", keyword, i));
                }

                taskModel.keyword = keyword;
                taskModel.product = product;
                taskModel.company = company;
                taskModel.html = htmls;
                taskModel.page = i;

                NumHelper.Instance.SetNumBD(1);    //统计搜索速度
                ThreadPool.SetMinThreads(2, 1);
                ThreadPool.SetMaxThreads(5, 3);
                ThreadPool.QueueUserWorkItem(new WaitCallback(CheckOrAdd), taskModel);//并发性过强
            }
        }

        void CheckOrAdd(object obj)
        {
            Model.TaskModel model = obj as Model.TaskModel;
            model.html = Regex.Replace(model.html, "<!--([\\s\\S].*?)-->", "").Trim();
            CheckOrAdd(model.html, model.keyword, model.product, model.company, model.page);
        }

        void CheckOrAdd(string html, string keyword, Model.Product product, Model.WebSite company, int page = 1)
        {
            NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html);
            Regex r1 = new Regex(FixRegex(company.ShortName), RegexOptions.IgnoreCase);
            Regex SiteName = new Regex(FixRegex(company.SiteName), RegexOptions.IgnoreCase);
            Regex Cellphone = new Regex(FixRegex(company.Cellphone), RegexOptions.IgnoreCase);
            Regex phone = new Regex(FixRegex(company.Phone), RegexOptions.IgnoreCase);

            CheckOrAdd(doc, r1, SiteName, Cellphone, phone, keyword, product, page);
        }

        private readonly object obj = new object();
        void CheckOrAdd(Document doc, Regex r1, Regex SiteName, Regex Cellphone, Regex phone, string keyword, Model.Product product, int page)
        {
            Element result = doc.GetElementById("results");
            if (result == null) return;

            NSoup.Select.Elements list = result.GetElementsByClass("result");
            HideElementList(result.GetElementsByClass("se-page-tips"));//顶部推荐
            HideElementList(result.GetElementsByClass("se-page-rcol"));//右边菜单
            HideElementList(result.GetElementsByClass("c-gap-bottom-small"));//小图

            //去掉百度广告
            if (Regex.IsMatch(result.ToString(), "ec_r_([\\s\\S].*?)\""))
            {
                Match mc = Regex.Match(result.ToString(), "ec_r_([\\s\\S].*?)\"");
                result.GetElementsByClass("ec_r_" + mc.Groups[1].Value).Remove();
            }

            int index = 0;
            //bool isFirst = true;
            foreach (Element ele in list)
            {
                index++;
                //匹配成功条件：简称|| 简称列表|| 勤加缘网站
                //勤加缘规则：标题有公司简称或者含有电话号码手机号码和公司简称 或者标题含有关键词和含有公司简称
                if (r1.IsMatch(ele.Text()) || CheckShortNameList(ele.Text()) || CheckQJY_BD(ele.Text(), SiteName, Cellphone, phone))
                {
                    string index_key = GetMd5(ele.Text()); //第几行的key,更新时，用来确定更新哪一行
                    string platform = GetPlatform(ele.Text());    //上词的平台
                    //if (isFirst)
                    //{
                        RemoveAttr(list); //删除链接
                    //    isFirst = false;
                    //}

                    //添加成功记录
                    lock (obj)
                    {
                        AddSnap(page, product.userid, product.itemid, index, keyword, result.Html(), platform, index_key);
                    }
                }
            }
        }

        /// <summary>
        /// 勤加缘匹配 百度
        /// </summary>
        bool CheckQJY_BD(string html, Regex SiteName, Regex Cellphone, Regex phone)
        {
            if (html.IndexOf("qjy168") == -1) return false;
            //公司全称、电话、手机匹配
            if (SiteName.IsMatch(html) || Cellphone.IsMatch(html) || phone.IsMatch(html))
                return true;
            return false;
        }

        void RemoveAttr(Elements result)
        {
            Elements style = result.Select("style");
            style.Remove();

            Elements link = result.Select("link");
            link.Remove();

            Elements tuijian = result.Select(".rs-toprq-wrapper-atom");
            tuijian.Remove();


            Elements div = result.Select("div");
            div.RemoveAttr("c-container");
            div.RemoveAttr("srcid");
            div.RemoveAttr("tpl");
            div.RemoveAttr("data-click");
            div.RemoveAttr("data-tools");
            div.RemoveAttr("id");
            div.RemoveAttr("data-server-rendered");
            div.RemoveAttr("data-log");//移除日志
            div.RemoveAttr("new_srcid");
            div.RemoveAttr("style");

            result.Select("firstpaint").Remove();
            result.Select("link").Remove();

            Elements a = result.Select("a");
            a.Attr("href", "#");
            a.RemoveAttr("data-sf-href");
            a.RemoveAttr("data-log");
            a.RemoveAttr("data-sf-options");
            a.RemoveAttr("data-url");
            a.RemoveAttr("data-sf-mip-preload");

            Elements span = result.Select("span");
            span.RemoveAttr("data-link");
            span.RemoveAttr("data-fclick");
            span.RemoveAttr("hint-type");

            Elements script = result.Select("script");
            script.Remove();

            //百度图片
            Elements bd_pic = result.Select(".result-op");
            bd_pic.Remove();

            Elements img = result.Select("img");
            img.Attr("src", "");

            Elements svg = result.Select("svg");
            svg.Remove();

            result.Select("firstpaint").Remove();
            result.Select("iframe").Remove();
            result.Select(".c-img").Remove();
            result.Select("b-scroll").Remove();
        }
    }
}